﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;

public partial class Admin_Position_PositionManager : System.Web.UI.Page
{
    PositionManager dllPositionManager = new PositionManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
            this.hId.Value = "0";
        }
    }


    /// <summary>
    /// 绑定GridView
    /// </summary>
    void BindGridView()
    {
        this.gdvPosition.DataSource = dllPositionManager.get();
        this.gdvPosition.DataBind();
    }

    /// <summary>
    /// 全选事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ckbAllSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (this.ckbAllSelect.Checked == true)
        {
            for (int i = 0; i < this.gdvPosition.Rows.Count; i++)
            {
                CheckBox ckb = (CheckBox)this.gdvPosition.Rows[i].Cells[0].FindControl("ckbSelect");
                ckb.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < this.gdvPosition.Rows.Count; i++)
            {
                CheckBox ckb = (CheckBox)this.gdvPosition.Rows[i].Cells[0].FindControl("ckbSelect");
                ckb.Checked = false;
            }
        }
    }

    /// <summary>
    /// 添加职务
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (this.hId.Value!="0")
        {
            object[] _value = new object[2];
            _value[0] = Convert.ToInt32(this.hId.Value);
            _value[1] = this.txtPositionName.Text.Trim();
            int result = dllPositionManager.edit(PositionManager.field.PositionId.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("修改职务未成功，请重试！");
            }
            else
            {
                Response.Redirect("PositionManager.aspx");
            }
        }
        else
        {

            object[] _value = new object[1];
            _value[0] = this.txtPositionName.Text;
            int result = dllPositionManager.add(_value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加职务未成功，请重试！");
            }
            else
            {
                Response.Redirect("PositionManager.aspx");
            }
        }
    }



    /// <summary>
    /// 新建事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddRole_Click(object sender, EventArgs e)
    {
        this.hId.Value = "0";
        this.txtPositionName.Text = "";
    }

    /// <summary>
    /// 删除事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click1(object sender, EventArgs e)
    {
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        for (int i = 0; i < this.gdvPosition.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvPosition.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                _count++;
                object[] _value = new object[1];
                _value[0] = Convert.ToInt32(this.gdvPosition.DataKeys[i]["PositionId"].ToString());
                _valueList.Add(_value);                
            }
        }
        if (_count == 0)
        {
        }
        if (_count > 0 && _valueList.Count > 0)
        {
            int result = dllPositionManager.delWithTransaction(PositionManager.field.PositionId.ToString(), _valueList);
            if (result < 0)
            {
                Utility.Error.RedirectErrorPage("删除职位未成功，请重试！");
            }
            else
            {
                Response.Redirect("PositionManager.aspx");
            }
        }
        else
        {
            Utility.HttpHelp.alert("请选择要删除的职务", Page);
        }
        
    }


    /// <summary>
    /// 行创建事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvPosition_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标进入行时
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
            //鼠标离开行时
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            //任意行都为选择行
            e.Row.Attributes.Add("onclick", "__doPostBack('gdvPosition','Select$" + e.Row.RowIndex + "')");
        }
    }

    /// <summary>
    /// 行选择事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvPosition_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        this.hId.Value = this.gdvPosition.DataKeys[e.NewSelectedIndex]["PositionId"].ToString();
        this.txtPositionName.Text = this.gdvPosition.Rows[e.NewSelectedIndex].Cells[2].Text;
        if (this.txtPositionName.Text == "&nbsp;")
        {
            this.txtPositionName.Text = "";
        }
    }

    /// <summary>
    /// 自动编号
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvPosition_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[1].Text = id.ToString();
        }
    }
}
